package com.tzxs.pay.main.deposit.pop;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialog;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CalendarView;
import android.widget.TextView;

import com.tzxs.pay.R;
import com.tzxs.pay.main.deposit.TypeNavigator;

public class OrderTimeFragment extends Fragment implements View.OnClickListener {
    private static OrderTimeFragment orderTimeFragment;
    //这个fragment传值的方法在哪里  我之前写的接口啊
    private TextView fastAll;
    private TextView fastToady;
    private TextView fastYes;
    private TextView fastThisWeek;
    private TextView fastThisMonth;
    private TextView fastLastMonth;
    //选择日期
    private TextView startTime;
    private TextView endTime;
    private String startTimeStr;//开始时间
    private String endTimeStr;//结束时间


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        setNavigator((TypeNavigator) context);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        return inflater.inflate(R.layout.pop_order_time, container, false);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        fastAll = view.findViewById(R.id.fast_choose_all);
        fastToady = view.findViewById(R.id.fast_choose_today);
        fastYes = view.findViewById(R.id.fast_choose_yesterday);
        fastThisWeek = view.findViewById(R.id.fast_choose_this_week);
        fastThisMonth = view.findViewById(R.id.fast_choose_this_month);
        fastLastMonth = view.findViewById(R.id.fast_choose_last_month);
        fastAll.setOnClickListener(this);
        fastAll.setSelected(true);
        fastToady.setOnClickListener(this);
        fastYes.setOnClickListener(this);
        fastThisWeek.setOnClickListener(this);
        fastThisMonth.setOnClickListener(this);
        fastLastMonth.setOnClickListener(this);


        startTime = view.findViewById(R.id.choose_conditions_start);
        endTime = view.findViewById(R.id.choose_conditions_end);
        TextView commit = view.findViewById(R.id.commit_sure);
        startTime.setOnClickListener(this);
        endTime.setOnClickListener(this);
        commit.setOnClickListener(this);


    }

    /**
     * 获取单例哦
     *
     * @return orderStatusFragment
     */
    public static OrderTimeFragment getInstance() {
        if (orderTimeFragment == null) {
            orderTimeFragment = new OrderTimeFragment();
        }
        return orderTimeFragment;
    }

    private TypeNavigator navigator;

    public void setNavigator(TypeNavigator navigator) {
        this.navigator = navigator;
    }

    /**
     * 清除选中状态
     */
    private void clearChoose() {
        fastAll.setSelected(false);
        fastToady.setSelected(false);
        fastYes.setSelected(false);
        fastThisWeek.setSelected(false);
        fastThisMonth.setSelected(false);
        fastLastMonth.setSelected(false);
    }

    @Override
    public void onClick(View v) {
        clearChoose();

        switch (v.getId()) {
            //不限
            case R.id.fast_choose_all:
                fastAll.setSelected(!fastAll.isSelected());
                setTimeChooseEmpty();
                navigator.getOrderTimeStr("不限");

                break;
            //今天
            case R.id.fast_choose_today:
                fastToady.setSelected(!fastToady.isSelected());
                setTimeChooseEmpty();
                navigator.getOrderTimeStr("今天");
                break;
            //昨天
            case R.id.fast_choose_yesterday:
                fastYes.setSelected(!fastYes.isSelected());
                setTimeChooseEmpty();
                navigator.getOrderTimeStr("昨天");//之前因为是现在换成fragment了
                break;
            //这周
            case R.id.fast_choose_this_week:
                fastThisWeek.setSelected(!fastThisWeek.isSelected());
                setTimeChooseEmpty();
                navigator.getOrderTimeStr("这周");
                break;
            //本月
            case R.id.fast_choose_this_month:
                fastThisMonth.setSelected(!fastThisMonth.isSelected());
                setTimeChooseEmpty();
                navigator.getOrderTimeStr("本月");
                break;
            //上月
            case R.id.fast_choose_last_month:
                fastLastMonth.setSelected(!fastLastMonth.isSelected());
                setTimeChooseEmpty();
                navigator.getOrderTimeStr("上月");
                break;
            //选择开始时间
            case R.id.choose_conditions_start:
                chooseTime(R.id.choose_conditions_start);

                break;
            //结束时间
            case R.id.choose_conditions_end:
                chooseTime(R.id.choose_conditions_end);

                break;
            //提交自定义时间确认
            case R.id.commit_sure:
                //将开始时间和结束时间用减号拼接
                navigator.getStartTime(startTimeStr);
                navigator.getEndTime(endTimeStr);

                break;
        }
    }

    /**
     * 选择快速选择时给自定义时间选择置空
     */
    private void setTimeChooseEmpty() {
        startTime.setText("");
        endTime.setText("");
    }

    /**
     * 选择时间
     *
     * @param id 控件id
     */
    private void chooseTime(final int id) {
        final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(getContext());
        View content = getLayoutInflater().inflate(R.layout.time_choose, null);
        CalendarView calendarView = content.findViewById(R.id.order_time_calendarView);
        bottomSheetDialog.setContentView(content);
        bottomSheetDialog.show();
        calendarView.setOnDateChangeListener((view, year, month, dayOfMonth) -> {
            bottomSheetDialog.dismiss();
            clearChoose();//清除 快速选择状态
            String mM;
            String day;
            if (dayOfMonth < 10) {//日
                day = "0" + String.valueOf(dayOfMonth);
            } else {
                day = String.valueOf(dayOfMonth);
            }

            if (month < 10) {
                mM = "0" + String.valueOf(month + 1);
            } else {
                mM = String.valueOf(month + 1);
            }
            switch (id) {
                case R.id.choose_conditions_start:
                    startTimeStr = year + "-" + mM + "-" + day;
                    startTime.setText(startTimeStr);
                    break;
                case R.id.choose_conditions_end:
                    endTimeStr = year + "-" + mM + "-" + day;
                    endTime.setText(endTimeStr);
                    break;
            }
        });
    }
}
